//
//  LXJRetweetedWeiboView.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/31.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit
import SnapKit

class LXJRetweetedWeiboView: UIView {
    
    // MARK: 微博的模型属性,并在控件上显示内容
    var retweetedWeibo: LXJWeiboViewModel? {
        didSet{
            /// 控件赋值
            retweetedWeiboContent.text = retweetedWeibo?.retweetedWeiboContent
            
            /// 转发图片视图的位置动态调整
            retweetedWeiboBottomConstraintFitForView(retweetedWeibo: retweetedWeibo)
        }
    }
    
    /// 转发图片视图的底部约束
    fileprivate var retweetedWeiboBottomConstraint: Constraint?
    
    // MARK: 控件的懒加载
    /// 转发微博内容
    fileprivate lazy var retweetedWeiboContent = UILabel(text: "此时的心情无比的烦躁,心塞,想死", font: 13, textColor: UIColor.darkGray, textAlignment: .left)
    ///转发的图片
    fileprivate lazy var pic_urlsView: LXJWeiboMultyPhotosView = LXJWeiboMultyPhotosView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /// 界面加载
        setupUI()
    }
    
    /// 不使用 XIB 加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 页面搭建
    fileprivate func setupUI() -> Void {
        /// 背景颜色
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        /// 添加子控件
        addSubview(retweetedWeiboContent)
        addSubview(pic_urlsView)
        
        /// 自动布局
        retweetedWeiboContent.snp.makeConstraints { (make) in
            make.top.leading.equalTo(homeWeiboCellmargin)
            make.trailing.equalTo(-homeWeiboCellmargin)
        }
        
        pic_urlsView.snp.makeConstraints { (make) in
            make.top.equalTo(retweetedWeiboContent.snp.bottom).offset(homeWeiboCellmargin)
            make.leading.equalTo(retweetedWeiboContent)
            // MARK: 暂时存在约束冲突问题 已解决
            make.size.equalTo(CGSize(width: 50, height: 50))
        }
        
        /// 根据转发内容自动匹配大小
        self.snp.makeConstraints { (make) in
            retweetedWeiboBottomConstraint = make.bottom.equalTo(pic_urlsView).offset(homeWeiboCellmargin).constraint
        }
    }
    
    /// 转发微博的图片约束调整
    fileprivate func retweetedWeiboBottomConstraintFitForView(retweetedWeibo: LXJWeiboViewModel?) -> Void {
        /// 卸载原来的约束条件
        retweetedWeiboBottomConstraint?.deactivate()
        
        /// 有转发图片
        if let pic_urls = retweetedWeibo?.homeWeiboList?.retweeted_status?.pic_urls, pic_urls.count > 0 {
            /// 显示转发微博的图片
            pic_urlsView.isHidden = false
            /// 控件赋值
            pic_urlsView.pic_urls = pic_urls
            /// 添加约束
            self.snp.makeConstraints { (make) in
                retweetedWeiboBottomConstraint = make.bottom.equalTo(pic_urlsView).offset(homeWeiboCellmargin).constraint
            }
        }else{/// 没有转发的图片
            /// 隐藏转发微博
            pic_urlsView.isHidden = true
            /// 添加约束
            self.snp.makeConstraints { (make) in
                retweetedWeiboBottomConstraint = make.bottom.equalTo(retweetedWeiboContent).offset(homeWeiboCellmargin).constraint
            }
        }
    }
}
